import { Logger } from "../engine/utils/Logger";
import RandomUtil from "../engine/utils/RandomUtil";
import path = require("path")
import fs = require("fs")
import { Config } from "./Config";
import { GameConfig } from "./GameConfig";

export class ServerConfig {

    public static Status_Type = {
        Normal: 1,
        Error: 998,
        Weihu: 999,
    }

    public serverType: string = ""

    public id: number = 0
    public ip: string;
    public internalIp: string = ""
    public http_port: number = 0;
    public socket_port: number = 0;
    public status: number = 1
    public nowPort: number = 0

    public onlineNum: number = 0

    public useridList: Array<number> = []


    public static upload_server_list: Array<ServerConfig> = []
    public static gm_server_list: Array<ServerConfig> = []
    public static pay_server_List: Array<ServerConfig> = []
    public static WebServer_List: Array<ServerConfig> = []
    public static account_server_List: Array<ServerConfig> = []
    public static hall_server_List: Array<ServerConfig> = []
    public static log_server_list: Array<ServerConfig> = []
    public static mj_server_list: Array<ServerConfig> = []

    public static nowServerConfig: ServerConfig

    public static async readConfigFile() {
        let configPath: string = path.resolve() + "/config/"
        if (Config.environment == Config.EnvironmentType.Dev) {
            configPath = configPath + "dev.json"
        }
        else if (Config.environment == Config.EnvironmentType.Test) {
            configPath = configPath + "test.json"
        }
        else if (Config.environment == Config.EnvironmentType.Online) {
            configPath = configPath + "online.json"
        }
        else {
            Logger.warn("不存在的环境配置文件", configPath)
        }
        // Logger.log("readConfigFile==", configPath)
        let data: Buffer = await fs.readFileSync(configPath);
        // Logger.log("readConfigFile=data=", data.toString())
        let configObj: object = JSON.parse(data.toString())
        return configObj
    }

    private static initOneServerType(configObj: object, nowPort: number) {
        let config: ServerConfig = new ServerConfig()
        config.ip = configObj["ip"];
        config.id = configObj["id"];
        config.internalIp = configObj["internalIp"]
        config.http_port = configObj["http_port"];
        config.socket_port = configObj["socket_port"];
        config.status = configObj["status"]
        if (nowPort == config.http_port || nowPort == config.socket_port) {
            ServerConfig.nowServerConfig = config;
            ServerConfig.nowServerConfig.nowPort = nowPort
        }
        return config;
    }

    public static async initConfig(nowPort: number) {
        let configObj: object = await this.readConfigFile()

        let serverList: Array<object> = configObj[Config.ServerType_List.WebServer];

        this.WebServer_List = []
        for (let i = 0; i < serverList.length; i++) {
            let config: ServerConfig = this.initOneServerType(serverList[i], nowPort);
            if (config.http_port == 9600) {
                this.WebServer_List[i] = config
                this.WebServer_List[i].serverType = Config.ServerType_List.WebServer
            }
        }

        serverList = configObj[Config.ServerType_List.gm_server]
        this.gm_server_list = []
        for (let i = 0; i < serverList.length; i++) {
            this.gm_server_list[i] = this.initOneServerType(serverList[i], nowPort)
            this.gm_server_list[i].serverType = Config.ServerType_List.gm_server
        }


        serverList = configObj[Config.ServerType_List.account_server]
        this.account_server_List = []
        for (let i = 0; i < serverList.length; i++) {
            this.account_server_List[i] = this.initOneServerType(serverList[i], nowPort);
            this.account_server_List[i].serverType = Config.ServerType_List.account_server
        }

        serverList = configObj[Config.ServerType_List.hall_server]
        this.hall_server_List = []
        for (let i = 0; i < serverList.length; i++) {
            this.hall_server_List[i] = this.initOneServerType(serverList[i], nowPort);
            this.hall_server_List[i].serverType = Config.ServerType_List.hall_server
        }

        serverList = configObj[Config.ServerType_List.log_server]
        this.log_server_list = []
        for (let i = 0; i < serverList.length; i++) {
            this.log_server_list[i] = this.initOneServerType(serverList[i], nowPort);
            this.log_server_list[i].serverType = Config.ServerType_List.log_server
        }

        serverList = configObj[Config.ServerType_List.mj_server]
        this.mj_server_list = []
        for (let i = 0; i < serverList.length; i++) {
            this.mj_server_list[i] = this.initOneServerType(serverList[i], nowPort);
            this.mj_server_list[i].serverType = Config.ServerType_List.mj_server
        }

        Logger.log("initConfig====mj_server_list==", this.mj_server_list)

    }

    public static randomWebServer() {
        return RandomUtil.randomItem(this.WebServer_List)
    }

    public static randomAccountServer() {
        return RandomUtil.randomItem(this.account_server_List)
    }

    public static randomHallServer() {
        return RandomUtil.randomItem(this.hall_server_List)
    }

    public static randomLogServer() {
        return RandomUtil.randomItem(this.log_server_list)
    }

    public static randomPayServer() {
        return RandomUtil.randomItem(this.pay_server_List)
    }

    public getHttpUrl() {
        return "http://" + this.ip + ":" + this.http_port
    }

    public getInternalUrl() {
        return "http://" + this.internalIp + ":" + this.http_port;
    }

    public getInternalWsUrl() {
        let url: string = "ws://" + this.internalIp + ":" + this.socket_port
        return url
    }

    public static randomGameServer(gameType: number) {
        let resultList: Array<ServerConfig> = []
        if (gameType == GameConfig.Game_Type.Mj) {
            resultList = this.mj_server_list
        }
        // else if (gameType == GameConfig.Game_Type.ddz) {
        //     resultList = this.ddz_server_list
        // }
        // else if (gameType == GameConfig.Game_Type.jianziding) {
        //     resultList = this.jianziding_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.cangxian_mj) {
        //     resultList = this.cangxian_mj_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.paodekuai) {
        //     resultList = this.paodekuai_server_list;
        // }
        // else if (gameType == GameConfig.Game_Type.dazhonger_mj) {
        //     resultList = this.dazhonger_mj_server_list;
        // }
        // resultList.sort((a: ServerConfig, b: ServerConfig) => {
        //     return a.useridList.length - b.useridList.length
        // })
        return resultList[0]
    }


    public static getAllSocketServer() {
        let result: Array<ServerConfig> = []
        result = result.concat(this.hall_server_List)
        // result = result.concat(this.huanghua_mj_server_list)
        // result = result.concat(this.ddz_server_list)
        // result = result.concat(this.jianziding_server_list)
        // result = result.concat(this.cangxian_mj_server_list)
        // result = result.concat(this.paodekuai_server_list)
        // result = result.concat(this.dazhonger_mj_server_list)
        return result
    }

    public static getAllGameServer() {
        let result: Array<ServerConfig> = []
        result = result.concat(this.mj_server_list)
        // result = result.concat(this.huanghua_mj_server_list)
        // result = result.concat(this.ddz_server_list)
        // result = result.concat(this.jianziding_server_list)
        // result = result.concat(this.cangxian_mj_server_list)
        // result = result.concat(this.paodekuai_server_list)
        // result = result.concat(this.dazhonger_mj_server_list)
        return result
    }

    public static getGameServerById(server_id: number) {
        let serverList: Array<ServerConfig> = this.getAllGameServer();
        for (let i = 0; i < serverList.length; i++) {
            let serverConfig: ServerConfig = serverList[i]
            if (serverConfig.id == server_id) {
                return serverConfig
            }
        }
        return null
    }


    public static getServerByIdAndType(server_id: number, serverType: string) {
        let nowServer: ServerConfig
        let serverList: Array<ServerConfig> = []
        // if (serverType == Config.ServerType_List.huanghua_mj_server) {
        //     serverList = this.huanghua_mj_server_list
        // }
        // else if (serverType == Config.ServerType_List.ddz_server) {
        //     serverList = this.ddz_server_list
        // }
        // else if (serverType == Config.ServerType_List.cangxian_mj_server) {
        //     serverList = this.cangxian_mj_server_list
        // }
        // else if (serverType == Config.ServerType_List.paodekuai_server) {
        //     serverList = this.paodekuai_server_list
        // }
        // else if (serverType == Config.ServerType_List.jianziding_server) {
        //     serverList = this.jianziding_server_list
        // }
        // else if (serverType == Config.ServerType_List.dazhonger_mj_server) {
        //     serverList = this.dazhonger_mj_server_list
        // }
        for (let i = 0; i < serverList.length; i++) {
            let server: ServerConfig = serverList[i]
            if (server.id == server_id) {
                nowServer = server
                break
            }
        }
        return nowServer
    }

    public static update_online(server_id: number, serverType: string, userids: Array<number>) {
        let nowServer: ServerConfig = this.getServerByIdAndType(server_id, serverType)
        if (nowServer) {
            nowServer.useridList = userids
            nowServer.onlineNum = userids.length
        }
    }

}